package com.sj.purchase.modules.purchase.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sj.purchase.common.utils.FileOperateUtil;
import com.sj.purchase.modules.purchase.service.MoveService;
import com.sj.purchase.modules.purchase.views.GyMoveStock;
import com.sj.purchase.util.excel.ExcelUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/move")
public class MoveController {

	@Autowired
	MoveService moveService;

	private static String DATA_FORMAT_STR = "yyyy-MM-dd";

	@RequestMapping("/html")
	public String url2() {
		return "move";
	}

	@RequestMapping("/getList")
	public String getList(@DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
						  @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime,
						  @DateTimeFormat(pattern = "yyyy-MM-dd") Date submitStartTime,
						  @DateTimeFormat(pattern = "yyyy-MM-dd") Date submitEndTime, String moveKey, String moveType, String referNo,
						  String warehouseNo, String storerNo, String codeValue,
						  @RequestParam(value = "isExcel", required = false, defaultValue = "false") boolean isExcel,
						  @RequestParam(value = "pageNum", required = false) String pageNum, String sourceZoneNo, String aimZoneNo,
						  Model model) {
		int pageSize = 20;
		int pageNum1 = 0;
		if (pageNum != null && !StringUtils.isEmpty(pageNum)) {
			pageNum1 = Integer.parseInt(pageNum);
		}
		JSONObject jsonObject = moveService.getList(startTime, endTime, submitStartTime, submitEndTime, moveKey,
				moveType, referNo, warehouseNo, storerNo, codeValue, sourceZoneNo, aimZoneNo, pageNum1, pageSize,
				isExcel);
		model.addAttribute("moveList", jsonObject);
		return "move";
	}

	@RequestMapping("/excel")
	public void getExcel(@DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
						 @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime,
						 @DateTimeFormat(pattern = "yyyy-MM-dd") Date submitStartTime,
						 @DateTimeFormat(pattern = "yyyy-MM-dd") Date submitEndTime, String moveKey, String moveType, String referNo,
						 String warehouseNo, String storerNo, String codeValue, String sourceZoneNo, String aimZoneNo,
						 HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<?, ?> map = moveService.getList(startTime, endTime, submitStartTime, submitEndTime, moveKey, moveType,
				referNo, warehouseNo, storerNo, codeValue, sourceZoneNo, aimZoneNo, 20, 0, true);
		Object object = map.get("content");
		List<GyMoveStock> list = JSON.parseArray(object.toString(), GyMoveStock.class);
		list.forEach(gyMoveStock->{
			if(gyMoveStock.getUpdateDate()!=null){
				gyMoveStock.setUpdateDateFormatStr(DateFormatUtils.format(gyMoveStock.getUpdateDate(),DATA_FORMAT_STR));
			}
			if(gyMoveStock.getCreateDate()!=null){
				gyMoveStock.setCreateDateFormatStr(DateFormatUtils.format(gyMoveStock.getCreateDate(),DATA_FORMAT_STR));
			}
		});
		FileOperateUtil.download(request, response, DateFormatUtils.format(startTime, "yyyy-MM-dd") + "yiku.xls",
				new ExcelUtil<GyMoveStock>(GyMoveStock.class).exportExcel(list, "订单信息"));

	}


	@RequestMapping("/newGetList")
	public ResponseEntity newGetList(Date startTime, Date endTime,Date submitStartTime,
						   Date submitEndTime, String moveKey, String moveType, String referNo,
						  String warehouseNo, String storerNo, String codeValue, String sourceZoneNo, String aimZoneNo,Pageable page) {
		JSONObject jsonObject = moveService.getList(startTime, endTime, submitStartTime, submitEndTime, moveKey,
				moveType, referNo, warehouseNo, storerNo, codeValue, sourceZoneNo, aimZoneNo,page.getPageNumber(), page.getPageSize(),
				false);

		String jsonArray = jsonObject.get("content")+"";
		List<GyMoveStock> gyMoveStockList = JSONArray.parseArray(jsonArray,GyMoveStock.class);
		int total = Integer.parseInt(jsonObject.get("totalElements")+"");
		return ResponseEntity.ok(new PageImpl<GyMoveStock>(gyMoveStockList,page,total));
	}


	@RequestMapping("/new/excel")
	public void newExcel(Date startTime, Date endTime,@RequestParam(required = false) Date submitStartTime,
						             @RequestParam(required = false) Date submitEndTime, String moveKey, String moveType, String referNo,
									 String warehouseNo, String storerNo, String codeValue, String sourceZoneNo, String aimZoneNo,
						             Pageable page,HttpServletRequest request, HttpServletResponse response) throws Exception  {
		JSONObject jsonObject = moveService.getList(startTime, endTime, submitStartTime, submitEndTime, moveKey,
				moveType, referNo, warehouseNo, storerNo, codeValue, sourceZoneNo, aimZoneNo,page.getPageNumber(), page.getPageSize(),
				true);
		String jsonArray = jsonObject.get("content")+"";

		List<GyMoveStock> gyMoveStockList = JSONArray.parseArray(jsonArray,GyMoveStock.class);
		gyMoveStockList.forEach(gyMoveStock->{
			if(gyMoveStock.getUpdateDate()!=null){
				gyMoveStock.setUpdateDateFormatStr(DateFormatUtils.format(gyMoveStock.getUpdateDate(),DATA_FORMAT_STR));
			}
			if(gyMoveStock.getCreateDate()!=null){
				gyMoveStock.setCreateDateFormatStr(DateFormatUtils.format(gyMoveStock.getCreateDate(),DATA_FORMAT_STR));
			}
		});
		FileOperateUtil.download(request, response, DateFormatUtils.format(startTime, "yyyy-MM-dd") + "yiku.xls",
				new ExcelUtil<GyMoveStock>(GyMoveStock.class).exportExcel(gyMoveStockList, "订单信息"));
	}


}
